package com.scheduling.product.controller;

import com.scheduling.common.result.Result;
import com.scheduling.product.dto.ProcessRouteDTO;
import com.scheduling.product.service.ProcessRouteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 工艺路线管理控制器
 *
 * @author 开发团队
 * @since 2025-05-29
 */
@Slf4j
@RestController
@RequestMapping("/api/process-route")
@Tag(name = "工艺路线管理", description = "工艺路线信息的增删改查操作")
@RequiredArgsConstructor
public class ProcessRouteController {

    private final ProcessRouteService processRouteService;

    @Operation(summary = "根据产品ID获取工艺路线", description = "根据产品ID获取工艺路线详情，包含工序列表")
    @GetMapping("/product/{productId}")
    public Result<ProcessRouteDTO> getRouteByProductId(
            @Parameter(description = "产品ID", required = true) @PathVariable String productId) {
        try {
            ProcessRouteDTO routeDTO = processRouteService.getRouteByProductId(productId);
            if (routeDTO == null) {
                return Result.success(null, "该产品暂无工艺路线");
            }
            return Result.success(routeDTO);
        } catch (Exception e) {
            log.error("根据产品ID获取工艺路线失败：{}", e.getMessage(), e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    @Operation(summary = "根据ID获取工艺路线详情", description = "根据工艺路线ID获取详细信息，包含工序列表")
    @GetMapping("/{routeId}")
    public Result<ProcessRouteDTO> getRouteById(
            @Parameter(description = "工艺路线ID", required = true) @PathVariable String routeId) {
        try {
            ProcessRouteDTO routeDTO = processRouteService.getRouteById(routeId);
            return Result.success(routeDTO);
        } catch (Exception e) {
            log.error("获取工艺路线详情失败：{}", e.getMessage(), e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    @Operation(summary = "创建工艺路线", description = "为产品创建新的工艺路线")
    @PostMapping
    public Result<String> createRoute(@Validated @RequestBody ProcessRouteDTO routeDTO) {
        try {
            boolean success = processRouteService.createRoute(routeDTO);
            if (success) {
                return Result.success("创建成功");
            } else {
                return Result.error("创建失败");
            }
        } catch (Exception e) {
            log.error("创建工艺路线失败：{}", e.getMessage(), e);
            return Result.error("创建失败：" + e.getMessage());
        }
    }

    @Operation(summary = "更新工艺路线", description = "更新工艺路线信息")
    @PutMapping
    public Result<String> updateRoute(@Validated @RequestBody ProcessRouteDTO routeDTO) {
        try {
            boolean success = processRouteService.updateRoute(routeDTO);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            log.error("更新工艺路线失败：{}", e.getMessage(), e);
            return Result.error("更新失败：" + e.getMessage());
        }
    }

    @Operation(summary = "删除工艺路线", description = "根据ID删除工艺路线，会同时删除关联的工序")
    @DeleteMapping("/{routeId}")
    public Result<String> deleteRoute(
            @Parameter(description = "工艺路线ID", required = true) @PathVariable String routeId) {
        try {
            boolean success = processRouteService.deleteRoute(routeId);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除工艺路线失败：{}", e.getMessage(), e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    @Operation(summary = "复制工艺路线", description = "将工艺路线复制到其他产品")
    @PostMapping("/copy")
    public Result<String> copyRoute(
            @Parameter(description = "源工艺路线ID", required = true) @RequestParam String sourceRouteId,
            @Parameter(description = "目标产品ID", required = true) @RequestParam String targetProductId,
            @Parameter(description = "新工艺路线名称", required = true) @RequestParam String routeName) {
        try {
            boolean success = processRouteService.copyRoute(sourceRouteId, targetProductId, routeName);
            if (success) {
                return Result.success("复制成功");
            } else {
                return Result.error("复制失败");
            }
        } catch (Exception e) {
            log.error("复制工艺路线失败：{}", e.getMessage(), e);
            return Result.error("复制失败：" + e.getMessage());
        }
    }

    @Operation(summary = "检查产品是否已有工艺路线", description = "检查产品是否已有工艺路线")
    @GetMapping("/check-product-route")
    public Result<Boolean> checkProductRouteExists(
            @Parameter(description = "产品ID", required = true) @RequestParam String productId,
            @Parameter(description = "排除的工艺路线ID（更新时使用）") @RequestParam(required = false) String excludeId) {
        try {
            boolean exists = processRouteService.checkProductRouteExists(productId, excludeId);
            return Result.success(exists);
        } catch (Exception e) {
            log.error("检查产品工艺路线失败：{}", e.getMessage(), e);
            return Result.error("检查失败：" + e.getMessage());
        }
    }
} 